package com.taxsystem.gaozhuanglin.booking;

import java.util.concurrent.ExecutionException;
import com.taxsystem.tool.HttpUtil;

import com.taxsystem.gaozhuanglin.bookingtool.Area;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.main.taxsystem.R;

public class BookActivity extends Activity {
	
	ListView areaList;
	String [] areaName = {"区域一", "区域二"};
	private int [] areaIds = {1,2};
	private static String url = "";
	private Area area1;
	public final static String RECI_COAST_AREA = "AREA_LIST";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_book);
		
		areaList = (ListView)findViewById(R.id.areas);
		
		
		
		IntentFilter filter = new IntentFilter(RECI_COAST_AREA);
		BroadcastReceiver myRecive = new AreaRecive();
		registerReceiver(myRecive, filter);
		
		Thread areaThread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST_AREA);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil.getRequest(url);
					intent.putExtra("areaName_list", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
		//areaThread.start();
		
		if(areaName==null)
		{Log.d("booking", "空指针异常");}
		else
		{
		ArrayAdapter<String> areaAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, areaName);
		areaList.setAdapter(areaAdapter);
		
		areaList.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Intent intent1 = new Intent(BookActivity.this, TicketItemActivity.class);
				int result1 = areaIds[position];
				intent1.putExtra("areaId", result1);
				startActivity(intent1);
			}
			
		});
		}
	}
	
	private class AreaRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try
			{
				JSONObject json = new JSONObject(intent.getStringExtra("areaName_list"));
				JSONArray jsonArray = json.getJSONArray("Data");
				Area [] areaArray = new Area[jsonArray.length()];
				areaName = new String[jsonArray.length()];
				areaIds = new int[jsonArray.length()];
				for(int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject j1 = (JSONObject)jsonArray.get(i);
					areaArray[i].setAreaId(j1.getInt("areaId"));
					areaArray[i].setAreaName(j1.getString("areaName"));
					areaArray[i].setMuId(j1.getInt("muId"));
					areaName[i] = j1.getString("areaName");
					areaIds[i] = j1.getInt("areaId");
				}
			}
			
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_tab, menu);
		return true;
	}

}
